<template>
	<view style="background-color: #FFFFFF;">
		<view class="cu-form-group margin-top">
			<view class="title">冲奶量：</view>
			<input type="number" v-model="babyconent.content" />
		</view>
		<view class="cu-form-group">
			<view class="title">实际量：</view>
			<input type="number" v-model="babyconent.realitycontent" />
		</view>
		<view class="cu-form-group">
			<view class="title">状 态：</view>
			<switch @change="SwitchA" :class="switchA?'checked':''" :checked="switchA?true:false">
			</switch>
		</view>
		<view class="cu-form-group">
			<view class="title">日期选择</view>
			<picker mode="date" :value="date" :start="startDate" :end="endDate" @change="DateChange">
				<view class="picker">
					{{date}}
				</view>
			</picker>
		</view>
		<view class="cu-form-group">
			<view class="title">时间选择</view>
			<picker mode="time" :value="time" start="09:01" end="21:01" @change="TimeChange">
				<view class="picker">
					{{time}}
				</view>
			</picker>
		</view>
		<view class="padding flex flex-direction">
			<button class="cu-btn bg-red margin-tb-sm lg" @click="editLog(id)">确定</button>
			<button class="cu-btn margin-tb-sm lg" @click="back">返回</button>
		</view>

		<text>说明：根据实际喂养情况，调整。如冲调120ml，实际可能只喝了90ml或者没喝</text>
	
	</view>
</template>

<script>
	import url_config from '@/common/config.js'
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				id: 0,
				babyconent: {
					realitycontent: 0,
					content: 0,
					status: 0
				},
				switchA: false,
				time: '12:01',
				date: currentDate,
			}
		},
		methods: {
			SwitchA(e) {
				//console.log(e.detail.value)
				//把布尔值转化成0/1.。。。呵呵哒
				if (e.detail.value) {
					this.babyconent.status = 1
				} else {
					this.babyconent.status = 0
				}

			},
			TimeChange(e) {
				this.time = e.detail.value
			},
			DateChange(e) {
				this.date = e.detail.value
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();

				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			editLog(id) {
				//修改记录，注意修改的数据中不能包含ID
				uni.request({
					url:  url_config.api + 'edit/' + id,
					method: 'PUT',
					data: this.babyconent,
					success: (res) => {
						//console.log(this.babyconent)
						uni.showToast({
							title: 'ok',
							duration: 2000
						})
					},
					fail: (err) => {
						console.log('err')
					}
				})
			},
			back(){
				//关闭当前页面后，跳转到指定页
				uni.redirectTo({
					url:'../baby/baby'
				})
			}
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}

		},
		onLoad: function(option) {
			this.id = option.id
			uni.request({
				//根据传过来到Id获取数据
				url: url_config.api + 'readone/' + option.id,
				success: (res) => {
					this.babyconent = res.data.data
					if (res.data.data.status == 1) {
						this.switchA = true
					} else {
						this.switchA = false
					}

				}
			})
		}
	}
</script>

<style>

</style>
